package cn.blb.service.edu.controller;


import cn.blb.common.result.Result;
import cn.blb.service.edu.entity.Course;
import cn.blb.service.edu.entity.Teacher;
import cn.blb.service.edu.entity.dto.CourseDto;
import cn.blb.service.edu.entity.vo.CourseInfoForm;
import cn.blb.service.edu.entity.vo.CourseQuery;
import cn.blb.service.edu.entity.vo.SubjectVo;
import cn.blb.service.edu.entity.vo.TeacherQuery;
import cn.blb.service.edu.service.CourseService;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ccl
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/edu/course")
@CrossOrigin
@Api(tags = "课程列表")
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping("save-course-info")
    public Result saveCourseInfo(@RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        boolean empty = StringUtils.isEmpty(courseId);
        return empty ? Result.error().setMessage("保存失败") : Result.ok().setData("courseId", courseId);
    }


    // 分页 + 条件查询
    @PostMapping("/{current}/{limit}")
    @ApiOperation(value = "获取所有课程")
    public Result pageList(@ApiParam("当前页") @PathVariable Long current,
                           @ApiParam("每页显示条数") @PathVariable Long limit,
                           @ApiParam("查询对象") @RequestBody CourseQuery courseQuery) {
        // 1.获取page对象
        Page<CourseDto> courseDtoPage = new Page<>(current, limit);

        QueryWrapper<CourseDto> wrapper = new QueryWrapper<>();
        wrapper.eq("c.is_deleted",0).eq("t.is_deleted",0);

        return courseService.getCourseList(courseDtoPage, wrapper, courseQuery);
    }

    @GetMapping("/info/{id}")
    public Result getCourseInfoFormById(@PathVariable String id) {
        CourseInfoForm courseInfoForm = courseService.getCourseInfoFormById(id);
        return courseInfoForm != null ? Result.ok()
                .setData("item", courseInfoForm) : Result.error().setMessage("数据自己跑了");
    }

    @PutMapping("courseInfoForm")
    public Result updateCourseInfo(@RequestBody CourseInfoForm courseInfo) {
        courseService.updateCourseInfoById(courseInfo);
        return Result.ok();
    }



    @PostMapping("{id}")
    public Result getCourseDtoById(@PathVariable String id) {
        CourseDto courseDto = courseService.getCourseDtoById(id);
        return Result.ok().setData("item", courseDto);
    }

    @PostMapping("publish/{id}")
    public Result publish(@PathVariable String id) {
        UpdateWrapper<Course> courseUpdateWrapper = new UpdateWrapper<>();
        courseUpdateWrapper.eq("id", id).set("status", Course.COURSE_NORMAL);
        return courseService.update(courseUpdateWrapper) ? Result.ok() : Result.error();
    }



    @PostMapping("info/{id}")
    public Result backCourseDtoById(@PathVariable String id){
        CourseDto courseDto = courseService.backCourseDtoById(id);
        return Result.ok().setData("item",courseDto);
    }

    @DeleteMapping("{id}")
    public Result removeCourseById(@PathVariable String id){
        return courseService.removeCourseById(id) ?Result.ok():Result.error().setMessage("删除失败");
    }



    @DeleteMapping("/remove")
    public Result remove(String id) {

        return Result.ok();
    }



}

